import React, { PureComponent } from "react";
import { Form, Input, Button, notification } from 'antd';
import { Password, Axios, Authority } from 'toolkit';
import styles from './index.css';
import router from 'umi/router';
import { DistrictItem, LoginInfo, Reslut } from '@/app';
import GlobalStore from '@/store';



enum ResultCode {
    "成功" = 1,
    "密码错误" = 2
}


class LoginPage extends PureComponent<any, any>{

    constructor(props: any) {
        super(props);
    }

    render() {

        const layout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 18 },
        };
        const tailLayout = {
            wrapperCol: { offset: 8 },
        };
        return (
            <div className={styles.normal} >
                <Form {...layout} className={styles.loginForm}
                    onFinish={this.submitLogin.bind(this)}
                >
                    <Form.Item label="账号" name="userCode" rules={[{ required: true, message: '请输入登陆账号!' }]}
                    >
                        <Input allowClear={true} ></Input>
                    </Form.Item>
                    <Form.Item label="密码" name="password" required={true}
                        rules={[{ required: true, message: '请输入登陆密码!' }]}
                    >
                        <Input.Password allowClear={true} visibilityToggle={true}></Input.Password>
                    </Form.Item>
                    <Form.Item  {...tailLayout} >
                        <Button type="primary" htmlType="submit"  >确定</Button>
                    </Form.Item>
                </Form>
            </div>
        )
    }

    submitLogin(values: any) {

        let questData = {
            userCode: values.userCode,
            password: values.password
        }

        Axios.post('/userLogin?isMobile=false', questData).then(resopnse => {
            let reslut = resopnse.data;
            if (!reslut.succeed) {
                notification.error({
                    message: '登陆错误',
                    description: reslut.message,
                });
                return;
            }

            let login: LoginInfo = reslut.data;

            if (login.resultCode === ResultCode.密码错误) {
                notification.error({
                    message: '登陆错误',
                    description: "密码错误",
                });
                return;
            }


            Authority.setToken(login.token);
            GlobalStore.setStore("LoginInfo", login);

            router.push("/");
        }).catch(error => {
            console.error(error);
            notification.error({
                message: '系统错误',
                description: error.message,
            });
        });


    }

    componentWillUnmount() {


    }

}


export default LoginPage;